//
//  Buy1DropView.swift
//  jiaoyi
//
//  Created by 易万军 on 2018/11/8.
//  Copyright © 2018 wjk. All rights reserved.
//

import UIKit


class Buy1DropDateView: YNDropDownView {
    
    var selectedBlock: ((String?,String?) -> ())?
    
    @IBOutlet weak var pickerContainer: UIView!
    
    @IBOutlet weak var datePicker: UIDatePicker!
    
    @IBOutlet weak var tipLabel: UILabel!
    private var beginDate: Date?
    private var endDate: Date?
    
    private var currentPickerIndex: Int = -1
    
    @IBOutlet weak var beginButton: UIButton!
    @IBOutlet weak var endButton: UIButton!
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
       
        datePicker.datePickerMode = .date
        datePicker.backgroundColor = UIColor.white
//        datePicker.locale = Locale.init(identifier: "zh-Hans")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        

        pickerContainer.alpha = 0
    }
    
    private func dateString(date: Date?) -> String?{
        guard  let date = date else {
            return nil
        }
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        return formatter.string(from: date)
    }
    
    @IBAction func dateDidSelected(_ sender: UIButton){
        
        let string = dateString(date: datePicker.date)
       
        if currentPickerIndex == 0 {
            beginDate = datePicker.date
            beginButton.setTitle(string, for: .normal)
//            self.selectEndDate(self.endButton)
            self.pickerContainer.alpha = 0
        }else{
            endDate = datePicker.date
            endButton.setTitle(string, for: .normal)
            self.doneAction(sender)
        }
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.hideMenu()
    }
    
    @IBAction func selectBeginDate(_ sender: UIButton) {
        currentPickerIndex = 0
        tipLabel.text = "请选择开始时间"
        sender.isHidden = false
        UIView.animate(withDuration: 0.25) {
            self.pickerContainer.alpha = 1
        }
        
    }
    
    @IBAction func selectEndDate(_ sender: Any) {
        currentPickerIndex = 1
        tipLabel.text = "请选择结束时间"
        UIView.animate(withDuration: 0.25) {
            self.pickerContainer.alpha = 1
        }
    }
    
    @IBAction func doneAction(_ sender: Any) {
        
        if let end = endDate, let begin = beginDate, end < begin{
            HUD.toast("结束日期不能小于开始日期", position: .bottom)
            return
        }
        self.hideMenu()
        selectedBlock?(dateString(date: beginDate),dateString(date: endDate))
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        self.initViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        self.initViews()
    }
    
//    @IBAction func confirmButtonClicked(_ sender: Any) {
//        self.hideMenu()
//
//    }
//    @IBAction func cancelButtonClicked(_ sender: Any) {
//        self.hideMenu()
//
//    }
    override func dropDownViewOpened() {
        print("dropDownViewOpened")
        
    }
    
    override func dropDownViewClosed() {
        print("dropDownViewClosed")
    
        
        
    }
    func initViews() {
        
    }
    
}


